package lc;

public class Q1138 {

    public static void main(String[] args) {
        Q1138 q1138 = new Q1138();
        System.out.println(q1138.alphabetBoardPath("leet"));
        System.out.println(q1138.alphabetBoardPath("code"));
        System.out.println(q1138.alphabetBoardPath("codze"));
    }

    public String alphabetBoardPath(String target) {
        char[] chars = target.toCharArray();
        int currentX = 0;
        int currentY = 0;
        StringBuilder sb = new StringBuilder();

        for (char aChar: chars) {
            int x = (aChar - 'a') % 5;
            int y = (aChar - 'a') / 5;
            // 优先向左和向上
            while (y < currentY) {
                sb.append("U");
                currentY --;
            }
            while (x < currentX) {
                sb.append("L");
                currentX --;
            }
            // 带z的重新处理
            while (y > currentY) {
                sb.append("D");
                currentY ++;
            }
            while (x > currentX) {
                sb.append("R");
                currentX ++;
            }
            sb.append("!");
        }
        return sb.toString();
    }
}
